<?php namespace Home\Controller; 


//确认订单控制器
class ConfirmController extends CommonController{

//	成员属性
	
	//构造函数
	public function __init()
	{
		parent::__init();
	}
	
    //动作
    public function index(){
    	
    	if(!IS_POST){
    		View::error('请提交购车数据',U('ShopCar/index'));
    	}
		
		
//		p($_POST);die;
		$glid = Q('post.glid',array());
//		p($glid);die;
		
//		调用获得购物车数据方法
		$data = $this -> getCartGoods($glid);
//		p($data);die;
		View::with('data',$data);
		
//		获取个人用户信息
		$uid = Q('session.uid');
		$userInfo = Db::table('snshop_User')
					-> where('uid',$uid)
					-> first();
		View::with('userInfo',$userInfo);
		
//		个人地址库
		$userAddr = Db::table('snshop_address')
					-> where('snshop_user_uid',$userInfo['uid'])
					-> get();
		View::with('userAddr',$userAddr);
		
    	View::make();
    }
	
	public function getCartGoods($glid){
		$data = Cart::getGoods();
//		p($data);die;
//		设定两个空数组，来接收已选商品数据
		$comData = array();
		$toalData = array();
//		数量
		$numData = array();
		
		foreach($data as $k => $v){
			if(in_array($k, $glid)){
				$comData['goods'][] = $v;
				
				$toalData[] = $v['total'];
				$numData[] = $v['num'];
			}
		}
//		p($toalData);die;
//		求和
		$comData['total_all'] = array_sum($toalData);
		$comData['total_num'] = array_sum($numData);
//		p($comData);die;

		return $comData;
	}
	
	
}
